home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Software Vault: The Gold Collection
/
Software Vault - The Gold Collection (American Databankers) (1993).ISO
/
cdr48
/
tpsecure.zip
/
SECURE.PAS
next >
Wrap
Pascal/Delphi Source File
|
1993-04-17
|
10KB
|
269 lines
unit Secure;
{===========================================================================+
| File: Secure.PAS |
| Project: Security for any Turbo Pascal Program |
+---------------------------------------------------------------------------+
| VER DATE AUTHOR - VERSION HISTORY, DESCRIPTION, or NOTES |
+---------------------------------------------------------------------------+
1.0 03/09/92 Jason Weaver : Original Version
----- DOCUMENTATION ---------------------------------------------------------
Development language/tools/libs: Turbo Pascal 6.0
Intructions and Notes:
This tiny unit is designed to illustarte how a secrity can be easily added
to any turbo pascal program. This secruity is by no means unbreakable, but
it will cause alot of pain and frustrarion on the part of hackers to figure
out. This unit could be extended by adding encription or other "deterants".
I claim no responsibility for any screw ups encountered by using this unit.
As of 03/09/92 it worked great. For more information, see the module
headers and TEST.PAS program.
===== END OF FILE HEADER ===================================================}
{$I-}
Interface
Uses DOS;
Type TStr12 = String[12];
TSecureRec = Record { RECORD STORED IN THE HIDDEN FILE }
CurrCount : Word; { COUNTER FOR NUMBER TIMES RUN }
MaxCount : Word; { MAXIMUM ALLOWABLE TIME TO RUN }
EXE_Time : LongInt; { TIME EXE FILE CREATED }
EXE_Size : LongInt; { SIZE OF EXE WHEN SETUP RUN }
end;
PSecurity = ^TSecurity;
TSecurity = Object { ERROR CODE, 0 IF NO ERROR }
ErrorCode : Byte;
Constructor Init(FName:TStr12); { INITIALIZE OBJECT WITH EXE NAME }
Function Installed:Boolean; { DETECTS IF KEY FILE PRESENT }
Function ValidUser:Boolean; { COMPARES COUNTER W/ MAXTIMES }
Function ErrorStr(Code:Byte):String; { RETURNS THE ERRORSTRING }
Procedure SetUp(CurrCount,MaxCount:Word); { SETS UP THE SECURITY }
Destructor Done; { ALL DONE }
PRIVATE
HiddenFile : PathStr; { NAME OF HIDDEN FILE }
EXEFile : PathStr; { NAME OF EXE FILE }
SecRec : TSecureRec; { SECURITY RECORD }
Procedure ReadFile(Var SecurityRec:TSecureRec);
Procedure WriteFile(Var SecurityRec:TSecureRec);
end;
Implementation
{---------------------------------------------------------------------------+
| Constructor: INIT |
+---------------------------------------------------------------------------+
Input: FName : Name of exe file to secure.
Description:
Initializes the hidden file name and exe file name.
----------------------------------------------------------------------------}
Constructor TSecurity.Init(FName:TStr12);
begin
HiddenFile := '-'+#255+#255+#255+#255+#255+#255+'-.-'+#255+'-';
HiddenFile := FExpand(HiddenFile);
EXEFile := FExpand(FName);
end;
Destructor TSecurity.Done;
begin
end;
{---------------------------------------------------------------------------+
| Function: Installed |
+---------------------------------------------------------------------------+
Returns: True if hidden file is locatable
Description:
Determines if the key file (hidden file) is available. If not,
the program should return "key not installed" or "key not found"
error.
----------------------------------------------------------------------------}
Function TSecurity.Installed:Boolean;
Var F : File;
begin
Assign(F,HiddenFile);
SetFAttr(F,Archive);
Reset(F);
if IOResult = 0
then begin
Installed := True;
Close(F);
SetFAttr(F,ReadOnly+Hidden);
end
else Installed := False;
end;
{---------------------------------------------------------------------------+
| Procedure: ReadFile |
+---------------------------------------------------------------------------+
Input: SecRec : Security record to be read from the Key file.
Output: SecRec : ...
Description:
Reads a file with attributes of Hidden and Read only.
----------------------------------------------------------------------------}
Procedure TSecurity.ReadFile(Var SecurityRec:TSecureRec);
Var F:File of TSecureRec;
begin
Assign(F,HiddenFile);
SetFAttr(F,Archive);
Reset(F);
Read(F,SecurityRec);
if IOResult <> 0
then ErrorCode := 002
else Close(F);
end;
{---------------------------------------------------------------------------+
| Procedure: WriteFile |
+---------------------------------------------------------------------------+
Input: SecRec : Security record to be written to disk.
Output: See input.
Description:
Writes the SecRec to the key file, which is granted the attributes
of hidden and read-only
----------------------------------------------------------------------------}
Procedure TSecurity.WriteFile(Var SecurityRec:TSecureRec);
Var F : File of TSecureRec;
begin
Assign(F,HiddenFile);
SetFAttr(F,Archive);
ReWrite(F);
Write(F,SecurityRec);
Close(F);
if IOResult <> 0
then ErrorCode := 001;
SetFAttr(F,ReadOnly+Hidden);
end;
{---------------------------------------------------------------------------+
| Procedure: SetUp |
+---------------------------------------------------------------------------+
Input: CurrCount : Count at which user is currently at. Typically starts
at zero, but some of you crazies my want it to vary, to
I left it open.
MaxCount : Make sure this value is greater then the CurrCount! Your
Program will have a short-life if done otherwise.
MaxCount is the maximum number to allow currcout to go to
Description:
Sets up the security program. WARNING..CAUTION!!! THIS PROCEDURE
SHOULD BE CALLED WITH CARE. EVERY CALL TO IT WILL RESET THE
COUNTS AND ALLOW THE USER ACCESS TO YOUR PROGRAM.
----------------------------------------------------------------------------}
Procedure TSecurity.SetUp(CurrCount,MaxCount:Word);
Var FileRec:SearchRec;
begin
FindFirst(EXEFile,AnyFile,FileRec);
if DosError <> 0
then begin
ErrorCode := 004;
Exit;
end;
if MaxCount < CurrCount
then begin
ErrorCode := 010;
Exit;
end;
SecRec.CurrCount := CurrCount;
SecRec.MaxCount := MaxCount;
SecRec.EXE_Size := FileRec.Size;
SecRec.EXE_Time := FileRec.Time;
WriteFile(SecRec);
end; { SetUp }
{---------------------------------------------------------------------------+
| Function: ValidUser |
+---------------------------------------------------------------------------+
Returns: True if user is valid, else returns false.
Description:
Checks Currcount against MaxCount
Checks for a date or time change in the Executable file (possible
virus protection).
----------------------------------------------------------------------------}
Function TSecurity.ValidUser:Boolean;
Var FileRec : SearchRec;
begin
ValidUser := True;
if Not Installed
then begin
ErrorCode := 002;
ValidUser := False;
Exit;
end;
ReadFile(SecRec);
Inc(SecRec.CurrCount);
FindFirst(EXEFile,Anyfile,FileRec);
if SecRec.CurrCount > SecRec.MaxCount then
begin
ErrorCode := 003;
ValidUser := False;
end
else
if (SecRec.EXE_Size <> FileRec.Size) or
(SecRec.EXE_Time <> FileRec.Time) then
begin
ErrorCode := 005;
ValidUser := False;
end;
WriteFile(SecRec);
end; { VALIDUSER }
{---------------------------------------------------------------------------+
| Function: TBD |
+---------------------------------------------------------------------------+
Input: Code : ErrorCode
Returns: String containing error code.
Description:
Returns an error string corresponding to ErrorCode (Code)
----------------------------------------------------------------------------}
Function TSecurity.ErrorStr(Code:Byte):String;
begin
Case Code of
000 : ErrorStr := 'No error(s) encountered.';
001 : ErrorStr := 'Unable to create necessary key.';
002 : ErrorStr := 'Unable to find security key.';
003 : ErrorStr := 'Security time expired.';
004 : ErrorStr := 'Unable to locate executable file.';
005 : ErrorStr := 'Executable file has been changed.';
006 : ErrorStr := 'Key file has been changed.';
010 : ErrorStr := 'Invalid SetUp parameter value(s).';
end; { case }
end;
end.
{---------------------------------------------------------------------------}
{//////////////////////////////END OF UNIT//////////////////////////////////}
{---------------------------------------------------------------------------}